package com.usian.controller.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.pojo.login.Login;
import com.usian.pojo.user.User;
import com.usian.service.FunctionService;
import com.usian.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private UserService userService;

    @Resource
    private FunctionService functionService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login/login";
    }

    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        List<User> userList = userService.list(new QueryWrapper<>());
        for (User u : userList) {
            if (u.getUserName().equals(user.getUserName()) && u.getPassword().equals(user.getPassword())){
                //model.addAttribute("userId",u.getUid());
                //return "login/toLoginHtml";

                //得到userId对应的user
                //User user = userService.getById(u.getUid());

                Integer rid = userService.decideRidByUid(u.getUid());

                //得到function表里的对应id
                List<Integer> fidList = functionService.decideFidByRid(rid);

                Login login = new Login(u.getUid(), user.getUserName(),rid,fidList);

                session.setAttribute("login", login);

                return "redirect:/shop/findAll";
            }
        }
        return "error/error_login";
    }

}
